package pkg

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"path/filepath"
	"time"
)

func LocalFileUpload(c *gin.Context) (string, error) {
	file, _ := c.FormFile("file")
	ext := filepath.Ext(file.Filename)

	n := time.Now()             // 当前时间戳
	dir := n.Format("20060102") // 日期目录

	// uploads/20230501/1234567890.png
	fileName := "uploads/" + dir + "/" + uuid.NewString() + ext
	err := c.SaveUploadedFile(file, "./"+fileName)
	if err != nil {
		return "", err
	}
	return fileName, nil
}
